// packageA/pages/operate/demonstration/demonstration.js
const App = getApp()
// let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;  //手机号正则表达式
let reg = /^1[0-9]{10}$/;
var reg_em = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //邮箱正则表达式
// import moment from '../../../../utils/moment'
import Toast from '../../../../weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: ['申请演示', '开户申请','申请结佣'],
    levelList:'',
    follow_form: {
      name: '',
      company_num: '',
      agent_name: '',
      agent_mobile: '',
      mobile: '',
    },
    fileList:[],
    nameR:'',
    open_form:{
      id:'',
      company_name:"",
      company_num: '',
      license_url: '',
      email:'',
      type:[],
    },
    show1:false,
    currentIndex: 0,
    actions1:[],
    actions: [
      {
        name: '20~99人',
      },
      {
        name: '100~499人',
      },
      {
        name: '500~999人',
      },
      {
        name: '1000~9999人',
      },
      {
        name: '10000人以上',
      },
    ],
    level: [
      // {
      //   name: '一级代理商',
      // },
      // {
      //   name: '一级代理商2',
      // },
    ],
    checkbox_list: [
      {id:1, title: '渠道代理', status: false },
      {id:2, title: '流量运营', status: false },
    ],
    api_root:''
  },

   //切换头部tabs
   handleTabS(e) {
    let time1 = new Date().getTime() + 24 * 60 * 60 * 1000
    let time2 = new Date().getTime()
    this.setData({
      ...this.data.follow_form,
      ...this.data.remind_form,
      'follow_form.remind_time': '',
      'remind_form.remind_time': '',
      currentIndex: e.currentTarget.dataset.index,
      currentIndex3: '',
      group_index: '',
      minDate: this.data.currentIndex == 0 ? time1 : time2
    })
    console.log(this.data.currentIndex)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var api_root = wx.getStorageSync('api_root');
    this.setData({
      api_root:api_root
    })
    // console.log(this.data.api_root);
    this.getList()
    this.getList2()
    options.tabid == 1 ? this.data.list.splice(3, 1) : this.data.list
    this.setData({
      list: this.data.list,
      ...this.data.remind_form,
      ...this.data.follow_form,
      ...this.data.deal_form,
      ...this.data.rollout_form,
      // 'remind_form.customer_id': options.id,
      // 'follow_form.customer_id': options.id,
      // 'deal_form.customer_id': options.id,
      // 'rollout_form.customer_id': options.id,
    })
  },
  handleSelect() {
    this.setData({
      show: true
    })
  },
  levelSelect() {
    this.setData({
      levelshow: true
    })
  },
  onClose() {
    this.setData(
      { 
        show: false,
        show1:false,
        levelshow:false
      }
      );
  },

  onSelect(event) {
    if(this.data.currentIndex == 0){
      this.setData({
        ...this.data.follow_form,
        ...this.data.open_form,
        'follow_form.company_num': event.detail.name,
        // 'follow_form.level': event.detail.name,
        'open_form.company_num': '',
      })
    }else if(this.data.currentIndex == 1){
      console.log(event)
      this.setData({
        ...this.data.follow_form,
        ...this.data.open_form,
        'follow_form.company_num': '',
        'open_form.company_num': event.detail.name,
      })
    }
  },

  onSelect1(event) {
    this.setData({
      nameR:event.detail.name,
      ...this.data.open_form,
      'open_form.id': event.detail.id,
    })
    console.log(this.data.nameR);
  },
  onSelectlevel(event) {
    console.log(event);
    this.setData({
      levelList:event.detail.name,
      ...this.data.follow_form,
      'follow_form.p_id': event.detail.id,
    })
  },

  onChange(event) {
    console.log(event)
    this.setData({
      ...this.data.open_form,
      'open_form.business_type': event.detail,
      result: event.detail
    });
  },

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },

  handleSaveForm() {
    if (this.data.currentIndex == 0) {
      this.applyBusinessDemo()
      return
    }
    if (this.data.currentIndex == 1) {
      this.applyBusinessAccount()
      return
    } 
  },
  inputOnChange(e) {
    let input = e.currentTarget.dataset.input
    let value = e.detail
    this.setData({
      follow_form: {
        ...this.data.follow_form,
        [input]: value
      },
      open_form: {
        ...this.data.open_form,
        [input]: value
      },
    })
   // console.log(this.data.open_form)
  },
  inputOnChange1(){
    this.setData({
      show1:true
    })
  },
  applyBusinessAccount(){
    let _this = this
    let { email} = _this.data.open_form
    console.log(_this.data.open_form)
    if (!reg_em.test(email)) {
      Toast.fail('邮箱格式有误');
      return false;
    }
    App._post('Business/applyBusinessAccount', _this.data.open_form, function (result) {
      if (result.code === 0) {
        Toast({
          type: 'success',
          message: '提交成功',
          onClose: () => {
            wx.redirectTo({
              url: '../../operate/account/accountlist',
            })
          },
        });
      } else {
        Toast.fail(result.msg);
      }
    });
  },
  applyBusinessDemo() {
    let _this = this
    let { mobile } = _this.data.follow_form
    if (!reg.test(mobile)) {
      Toast.fail('手机号不符合要求');
      return false;
    }
    App._post('Business/applyBusinessDemo', _this.data.follow_form, function (result) {
      // console.log(result);
      // return false;
      if (result.code === 0) {
        Toast({
          type: 'success',
          message: '提交成功',
          onClose: () => {
            // wx.switchTab({
            //   url: '../index/index?val=2',
            // })
            // wx.navigateTo({
            //   url: '../../operate/demonstration/demonstration-list',
            // })
            wx.reLaunch({
              url:'../../operate/index/index'
            })
          },
        });
      } else {
        Toast.fail(result.msg);
      }
    });
  },
  getList(){
    let _this = this
    App._post_form('Business/getBusinessOpen', {
      action:'account_open_list'
    }, function (result) {
      if (result.code === 0) {
        let arr = result.data.data
        let newArr = []
        for (let index = 0; index < arr.length; index++) {
          let obj = {
            name:arr[index].name,
            id:arr[index].id
          }
          newArr.push(obj)
        }
        _this.setData({
          actions1:newArr
        })
        // _this.setData({
        //   level:newArr
        // })
      }
    });
  },

  getList2(){
    let _this = this
    App._post_form('Business/getBusinessOpen', {
      action:'apply_open'
    }, function (result) {
      if (result.code === 0) {
        let arr = result.data.data
        let newArr = []
        let yiji = {name:"一级加盟商",id:0}
        for (let index = 0; index < arr.length; index++) {
          let obj = {
            name:arr[index].name,
            id:arr[index].id
          }
          newArr.push(obj)
        }
        _this.setData({
          level:newArr
        })
        console.log(_this.data.level);
      }
    });
  },


  load(){
    let _that = this;
    wx.downloadFile({
      url:'https://p.t.fangx999.com/download_files/AccountStatement.xls', //仅为示例，并非真实的资源
      success(res) {
        console.log(res)
        
        var filePath = res.tempFilePath;
        wx.saveFile({
          tempFilePath: filePath,
          success: (res) => {
            console.log('读取成功', res)
            var saveFilePath = res.savedFilePath
            wx.openDocument({
              filePath: saveFilePath,
              showMenu: true, // 必须写新开页面打开文档。微信客户端 7.0.12 版本前默认显示右上角菜单按钮，之后的版本默认不显示，需主动传入 showMenu
              fileType: "xls",
              //就是之前的那个saveFilePath
              success: function (res) {
                console.log("打开成功!", res)
              }
            })
          },
          fail: (err) => {
            console.log('读取失败', err)
          }
        })
      }
    })
  },
//图片上传
afterRead(event) {
  let api_root = wx.getStorageSync('api_root')
    if (api_root) {
      App.api_root = api_root;
    }
  const { file } = event.detail;
  console.log(file)
  // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
  let _this = this
  let imgList = []
  wx.uploadFile({
    url: App.api_root+'Customer/imageUpload', // 仅为示例，非真实的接口地址
    filePath: file.url,
    name: 'proof',
    success(res) {
      console.log(res)
      // 上传完成需要更新 fileList

      const { fileList = [] } = _this.data;
      fileList.push({ ...file, url: JSON.parse(res.data).data.url });
      imgList = fileList.map(item => {

        return item.url
      })
      imgList = imgList.join(',')
      console.log(imgList)
      _this.setData({
        fileList,
        ..._this.data.open_form,
        'open_form.license_url': imgList
      });
    },
  });
},
   //删除
   deleteImg(e) {
    const index = e.detail.index
    let imgList = []
    this.data.fileList.splice(index, 1)
    imgList = this.data.fileList.map(item => {
      return item.url
    })
    imgList = imgList.join(',')
    console.log(imgList)
    this.setData({                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
      fileList: this.data.fileList,
      ...this.data.summary_form,
      'summary_form.proof': imgList
    })
    console.log(e, this.data.fileList)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})